<template>
	<view class="">
		<!-- <uni-nav-bar :fixed="true" left-icon="back"  :border="false"backgroundColor="#f9f2ea" leftText="申请授权" :statusBar="true" @clickLeft="goBack"></uni-nav-bar> -->
		<image class="img" src="../../static/logo.jpg" mode=""></image>
		<view class="title">
			登录啊器商城
		</view>
		<view class="tips">
			玩出自己的艺术品
		</view>
		<button class="btn" type="primary" open-type="getUserInfo" @tap="getUserInfo">微信授权</button>
		<view class="rule">
			<view class="">
				<checkbox-group @change="doAgree">
					<label>
						<checkbox value="agree" />
					</label>
				</checkbox-group>
			</view>
			<view class="">
				登陆即代表同意
			</view>
			<view class="blue" @click="toUserAgreement">
				用户协议
			</view>
			<view class="">
				和
			</view>
			<view class="blue" @click="toPrivacyPolicy">
				隐私政策
			</view>
		</view>
		<!-- <button class='btn' type="primary" open-type="getUserInfo" @getuserinfo="getuserinfo" withCredentials="true">微信一键登录</button> -->
	</view>
</template>

<script>
	import {
		userInfo,
		wxLogin,
		updateUserInfo,
		getAddressData
	} from "@/api/public.js"
	export default {
		data() {
			return {
				userData: {},
				type: "",
				agree: false,
			}
		},
		onShow() {
			if (uni.getStorageSync("userData")) {
				let _this = this;
				if (uni.getStorageSync("sessionId")) {
				} else {
				
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							wxLogin(loginRes.code).then(res => {
								uni.setStorageSync("sessionId", res.reData.sessionId)
								uni.reLaunch({
									url: "choose"
								})
							});
						},
					});
				}
			}
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			toUserAgreement(){
				uni.navigateTo({
					url: "./userAgreement"
				})
			},
			toPrivacyPolicy(){
				uni.navigateTo({
					url: "./privacyPolicy"
				})
			},
			doAgree(e){
				console.log(e.detail.value)
				if(e.detail.value.length == 1){
					this.agree = true
				}else{
					this.agree = false
				}
			},
			// getUserInfo() {
			// 	wx.getUserProfile({
			// 		desc: 'weixin',
			// 		success: (infoRes)=>{
			// 			uni.setStorageSync("userInfo", JSON.stringify(infoRes.userInfo))
			// 			updateUserInfo(infoRes.encryptedData, infoRes.iv).then(res=>{
			// 				userInfo().then(res=>{
			// 					if(res.reData.data){
			// 						_this.userData = res.reData.data;
			// 						uni.setStorageSync("userData", JSON.stringify(_this.userData));
			// 					}
			// 					uni.reLaunch({
			// 						url: "choose"
			// 					})
			// 				})
			// 			})
			// 		}
			// 	});
			// },
			getUserInfo() {
				if(this.agree){
					
				}else{
					uni.showToast({
						icon: "none",
						title: "请先阅读并同意协议"
					})
					return false
				}
				let _this = this;
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						wxLogin(loginRes.code).then(res => {
							uni.setStorageSync("sessionId", res.reData.sessionId);
							uni.reLaunch({
								url: "/pages/index/choose"
							})
						});
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.img {

		width: 240rpx;
		height: 240rpx;
		border-radius: 120rpx;
		margin: 20vh auto 60rpx;
	}

	.title {
		font-size: 32rpx;
		line-height: 48rpx;
		color: #333;
		text-align: center;
	}

	.tips {
		font-size: 28rpx;
		line-height: 40rpx;
		color: #666;
		text-align: center;
		margin-bottom: 100rpx;
	}

	.btn {
		width: 600rpx;
		height: 80rpx;
		border-radius: 40rpx;
		text-align: center;
		color: #fff;
		font-size: 36rpx;
		line-height: 80rpx;
	}
	.rule{
		width: 600rpx;
		margin: 20rpx auto;
		display: flex;
		font-size: 24rpx;
		align-items: center;
		.blue{
			color: blue;
		}
	}
</style>
